﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using log4net;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form._03_Order
{
    public partial class OrderPremiumForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(OrderPremiumForm));
        private Timer _timer = new Timer();
        public string Date { get; set; }

        public OrderPremiumForm()
        {
            InitializeComponent();
        }

        private void OrderPremiumForm_Load(object sender, EventArgs e)
        {
            _timer.Interval = 100;
            _timer.Tick += _timer_Tick;

            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Value";
        }

        private void OrderPremiumForm_Shown(object sender, EventArgs e)
        {
            listBox1.Height = Height - (Padding.Top + Padding.Bottom) - panel1.Height - 30;
            _timer.Start();
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            try
            {
                _timer.Stop();
                var shops = ShopService.QueryAll();
                var ls = shops.Select(x => ShopInfo_2_ComboBoxItemData(x)).ToList();
                comboBox1.Items.AddRange(ls.ToArray());
                comboBox1.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private ComboBoxItemData<int> ShopInfo_2_ComboBoxItemData(ShopInfoEntity si)
        {
            return new ComboBoxItemData<int>
            {
                Value = si.ShopManagerUserId,
                Text = si.Name
            };
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string date = dateTimePicker1.Value.ToString("yyyy-MM-dd");
                int shopManagerUserId = (comboBox1.SelectedItem as ComboBoxItemData<int>).Value;
                var ls = OrderService.QueryByShopManagerUserId(shopManagerUserId, date);
                var ls2 = ls.OrderBy(x => x.OrderInfo.Amount).Select(x =>
                  {
                      string str = string.Format("{0}:\t{1}", x.UserInfo.Name
                                      , Math.Round(x.OrderInfo.Amount * 1.025M, 2));
                      return str;
                  }).ToList();
                listBox1.BeginUpdate();
                listBox1.Items.Clear();
                listBox1.Items.AddRange(ls2.ToArray());
                listBox1.EndUpdate();
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }
    }
}
